﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Systems.Log
{
    public partial class LogManager : System.Web.UI.UserControl
    {
        #region check login and permission 
            InPms chkPms = new InPms();
            public Int32 panelId = 0;
            private void checkLogin()
            {
                // Check and load UC
                if (Request.QueryString["ui"] != null)
                    Int32.TryParse(Request.QueryString["ui"], out panelId);
                // check Pms
                if (panelId > 0)
                    chkPms = InPmsBusiness.CheckSessionPms(panelId);
                if (chkPms == null)
                    Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
                // show Pms in Top Bar
                TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
                ucTopBar.BindData(chkPms);
            }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindAction();
                    BindData();
                }
            }
            
        }


        public string mail, from, to = string.Empty;
        public Int32 action = -1;
        public Int32 pIndex = 1;
        public Int32 pSize = 20;
        public Int32 totalR = 0;
        private void BindData()
        {
            mail = Utils.GetQuery("mail", "");
            from = Utils.GetQuery("from", "");
            to = Utils.GetQuery("to", "");
            action = Utils.GetQuery("action", -1);
            pIndex = Utils.GetQuery("page", 1);
            DateTime dFrom = new DateTime(2014, 1, 1);
            DateTime dTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(1);

            if (!(String.IsNullOrEmpty(from)))
                dFrom = DateTime.ParseExact(from, "d/M/yyyy", null);

            if (!(String.IsNullOrEmpty(to)))
                dTo = DateTime.ParseExact(to, "d/M/yyyy", null).AddDays(1);

            // bind stt to GUI
            ddlAction.SelectedValue = action.ToString();


            var lst = InLogBusiness.GetInLogListByPaging(mail, action, dFrom, dTo, pIndex, pSize, ref totalR);
            if (lst.Count > 0)
            {
                rpt.DataSource = lst;
                //rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                rpt.DataBind();
            }
            Pager.Index = pIndex;
            Pager.Cate = Regex.Replace(Utils.GetCurrentCmsPage(), "&page=(.*)", "");
        }


        // Load cac Action vao Drop down list
        private void BindAction()
        {
            ddlAction.Items.Add(new ListItem("-- No Filter --", "-1"));
            ddlAction.Items.Add(new ListItem("Xóa / Khóa / Ẩn", "0"));
            ddlAction.Items.Add(new ListItem("Thêm mới", "1"));
            ddlAction.Items.Add(new ListItem("Cập nhật", "2"));
            ddlAction.Items.Add(new ListItem("Thao tác khác (Login, Upload, Sắp xếp)", "3"));
        }
    }
}